/*
  SOLID - Interference Detection Library
  Copyright (C) 1997 Gino van den Bergen

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

  Please send remarks, questions and bug reports to gino@win.tue.nl,
  or write to:
                  Gino van den Bergen
		  Department of Mathematics and Computing Science
		  Eindhoven University of Technology
		  P.O. Box 513, 5600 MB Eindhoven, The Netherlands
*/

#include "Detector.h"

ShapeList Detector::shapeList;
ObjectList Detector::objectList;
RespTable Detector::respTable;
ProxList Detector::proxList;

PointBuf Detector::pointBuf;
PointBuf Detector::report;

Shape *Detector::currentShape = 0;
Object *Detector::currentObject = 0;


// switches
bool Detector::nbody = true;

bool Detector::object_test(const Object& obj1, const Object& obj2) {
  const Response& resp = respTable.find(obj1.ref, obj2.ref);
  switch (resp.type) {
  case DT_SIMPLE_RESPONSE:
    if (intersect(obj1, obj2)) {
      resp(obj1.ref, obj2.ref);
      return true; 
    }
    break;
  case DT_SMART_RESPONSE:
    if (intersect(obj1, obj2, report)) {
      Response::coll_data.numverts = report.size();
      Response::coll_data.verts = (DtScalar (*)[3])&report[0];
      resp(obj1.ref, obj2.ref);
      return true;
    }
    break;
  }
  return false;
}

int Detector::test() {
  move();
  int count = 0;
  if (nbody) {
    for (ProxList::const_iterator i = proxList.begin(); 
	 i != proxList.end(); ++i)
      if (object_test(*(*i).first, *(*i).second)) ++count;
  }
  else {
    for (ObjectList::const_iterator j = objectList.begin(); 
	 j != objectList.end(); ++j)
      for (ObjectList::const_iterator i = objectList.begin(); 
	   i != j; ++i)
	if (object_test(*(*i).second, *(*j).second)) ++count;
  }
  return count;
}


